from tkinter import *

class UIComponents:
    def __init__(self, root, canvas):
        self.root = root
        self.canvas = canvas
        self.buttons = {}
        self._create_track_layout()
        self._create_insulation_joints()
        self._create_labels()
    
    def _create_track_layout(self):
        """创建轨道布局"""
        # 主要轨道线
        self.canvas.create_line(100, 150, 1500, 150, width=3, smooth=True)  # I
        self.canvas.create_line(100, 450, 1500, 450, width=3, smooth=True)  # III
        self.canvas.create_line(300, 300, 700, 300, width=3, smooth=True)   # II
    
    def _create_insulation_joints(self):
        """创建绝缘结"""
        insulation_points = [
            (370, 145, 370, 155), (650, 145, 650, 155), (900, 145, 900, 155),
            (1050, 145, 1050, 155), (1450, 145, 1450, 155), (350, 295, 350, 305),
            (480, 295, 480, 305), (120, 445, 120, 455), (1100, 445, 1100, 455),
            (820, 445, 820, 455), (490, 445, 490, 455)
        ]
        
        for x1, y1, x2, y2 in insulation_points:
            self.canvas.create_line(x1, y1, x2, y2, width=3, smooth=True)
    
    def _create_labels(self):
        """创建标签"""
        labels = [
            (870, 165, "6"), (140, 430, "1"), (650, 315, "2"), (760, 430, "4"),
            (1020, 75, "6道岔"), (1090, 75, "2/4道岔")
        ]
        
        for x, y, text in labels:
            self.canvas.create_text(x, y, text=text, font=("黑体", 12), fill="black")
    
    def create_button(self, name, text, command, x, y, width=100, height=40, bg="blue", fg="white"):
        """创建按钮"""
        button = Button(self.root, text=text, fg=fg, command=command, bd='2', bg=bg)
        button.place(width=width, height=height, x=x, y=y)
        self.buttons[name] = button
        return button
    
    def create_signal_button(self, name, text, command, x, y):
        """创建信号按钮"""
        return self.create_button(name, text, command, x, y, width=25, height=25)
    
    def create_control_buttons(self, section_manager, station_controller):
        """创建控制按钮"""
        # 道岔控制按钮
        self.create_button("ZDW", "设置定位", lambda: self._set_switch_normal(), 1300, 30, 70, 40)
        self.create_button("ZFW", "设置反位", lambda: self._set_switch_reverse(), 1410, 30, 70, 40)
        self.create_button("ZQX", "取消进路建立", lambda: self._cancel_route(), 1170, 30, 100, 40)
        
        # 区段控制按钮
        self.create_button("SetOccupancy", "设置区段占用", 
                          lambda: section_manager.enter_selection_mode("set"), 100, 520, 100, 40)
        self.create_button("CancelOccupancy", "取消区段占用", 
                          lambda: section_manager.enter_selection_mode("cancel"), 230, 520, 100, 40)
        self.create_button("CancelSelection", "取消选择模式", 
                          lambda: section_manager.exit_selection_mode(), 360, 520, 100, 40, bg="red")
        
        # 站台控制按钮
        self.create_button("SetStation", "设置站台门紧急关闭状态", 
                          lambda: station_controller.set_emergency_state(), 490, 520, 180, 40)
        self.create_button("CancelStation", "取消站台门紧急关闭状态设置", 
                          lambda: station_controller.cancel_emergency_state(), 700, 520, 200, 40)
        self.create_button("SetSt", "站台门打开", 
                          lambda: station_controller.open_station_door(), 930, 520, 100, 40)
        self.create_button("CancelSt", "站台门关闭", 
                          lambda: station_controller.close_station_door(), 1060, 520, 100, 40)
        
        # 信号按钮
        self.create_signal_button("XC", "XC", lambda: self._click_XC(), 905, 108)
        self.create_signal_button("F4", "F4", lambda: self._click_XC(), 375, 108)
        self.create_signal_button("F8", "F8", lambda: self._click_XC(), 1455, 108)
        self.create_signal_button("F5", "F5", lambda: self._click_F5(), 450, 318)
        self.create_signal_button("F2", "F2", lambda: self._click_F5(), 355, 257)
        self.create_signal_button("F6", "F6", lambda: self._click_F6(), 825, 408)
        self.create_signal_button("F1", "F1", lambda: self._click_F6(), 90, 468)
        self.create_signal_button("F3", "F3", lambda: self._click_F6(), 460, 468)
        self.create_signal_button("SC", "SC", lambda: self._click_F6(), 1070, 468)
    
    def _set_switch_normal(self):
        """设置道岔定位"""
        # 这里需要连接到道岔控制器
        pass
    
    def _set_switch_reverse(self):
        """设置道岔反位"""
        # 这里需要连接到道岔控制器
        pass
    
    def _cancel_route(self):
        """取消进路建立"""
        # 这里需要连接到进路控制器
        pass
    
    def _click_XC(self):
        """点击XC信号"""
        # 这里需要连接到信号控制器
        pass
    
    def _click_F5(self):
        """点击F5信号"""
        # 这里需要连接到信号控制器
        pass
    
    def _click_F6(self):
        """点击F6信号"""
        # 这里需要连接到信号控制器
        pass 